home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / mflzt.exe / lha / GTOD.C < prev    next >
C/C++ Source or Header  |  1989-10-30  |  7KB  |  227 lines

  1. /*
  2. **                GET TIME OF DAY UTILITY
  3. **
  4. ** S.E. Margison  Copyright 1987  All rights reserved
  5. ** 11-08-88 A  V1.05  Turbo-C 2.0
  6. **
  7. **  Options default to date, time, 12 hour format, no seconds, American
  8. **          style reporting.  Either - or / used for options, or none.
  9. **  -d   report (D)ate only
  10. **  -t   report (T)ime only
  11. **  -s   report (S)econds (only if -t option used)
  12. **  -l   Report date in (L)ong format
  13. **  -n   Include the (N)ame of the weekday, if -l format
  14. **  -m   report time in (M)ilitary format (12 hour format if not used)
  15. **  -e   report date in (E)uropean format
  16. **  -r   for time and date reporting, (R)everse order to time first
  17. **  If any options are used, no default values are assumed.
  18. **  No options is the same as -dt
  19. **
  20. **   As distributed, this program requires (for compilation):
  21. **     "Steve's Turbo-C Library" version 1.41 or later
  22. **   which may be obtained without registration from many Bulletin
  23. **   Board Systems including:
  24. **      Compuserve IBMSW
  25. **      GEnie
  26. **   and software library houses including:
  27. **      Public (Software) Library (Houston, TX.)
  28. **
  29. **   or by registration:
  30. **      $25 for Docs, C, S, M, L, H libraries, and complete library source
  31. **              in C and Assembler
  32. **     Steven E. Margison
  33. **     124 Sixth Street
  34. **     Downers Grove, IL, 60515
  35. **
  36. */
  37.  
  38. #include <stdio.h>
  39. #include <time.h>
  40. #include <mfltime.h>
  41.  
  42. int nf, tf, df, sf, mf, rf, ef, lf;   /* option flags */
  43.  
  44. char datestr[64],   /* place for formatted date string */
  45.      timestr[16];   /* place for formatted time sring */
  46.  
  47. extern char *wkdayname();
  48. extern char *monthis();
  49.  
  50. struct tm *ptm;
  51. long clockval;
  52.  
  53. main(argc, argv)
  54. int argc;
  55. char *argv[];
  56. {
  57.  
  58.     /* first thing to do is read the clock! */
  59.  
  60.    time(&clockval);
  61.    ptm = localtime(&clockval);
  62.    doargs(argc, argv);  /* parse any options */
  63.  
  64.    if(!tf && !df) tf = df = TRUE;
  65.  
  66.    if(!tf) timestr[0] = NULL;   /* skip the time */
  67.    else MKtime();
  68.  
  69.    if(!df) datestr[0] = NULL;   /* skip the date */
  70.    else MKdate();
  71.  
  72.    if(!rf) printf("%s  %s\n", datestr, timestr);
  73.    else printf("%s  %s\n", timestr, datestr);
  74. }
  75.  
  76. doargs(argc, argv)
  77. int argc;
  78. char *argv[];
  79. {
  80.    int i;
  81.    nf = tf = df = sf = mf = rf = ef = lf = FALSE;
  82.    while(--argc > 0) {
  83.       i = 0;
  84.       while(argv[argc][i] != NULL) {
  85.          switch(tolower(argv[argc][i++])) {
  86.             case '-': case '/':
  87.                break;
  88.             case 'n':
  89.                nf = TRUE;
  90.                break;
  91.             case 'd':
  92.                df = TRUE;
  93.                break;
  94.             case 't':
  95.                tf = TRUE;
  96.                break;
  97.             case 's':
  98.                sf = TRUE;
  99.                break;
  100.             case 'm':
  101.                mf = TRUE;
  102.                break;
  103.             case 'r':
  104.                rf = TRUE;
  105.                break;
  106.             case 'e':
  107.                ef = TRUE;
  108.                break;
  109.             case 'l':
  110.                lf = TRUE;
  111.                break;
  112.             case 'v':
  113.                fputs("GTOD Version 1.05\n", stderr);
  114.                fputs("GTOD ? for usage message\n", stderr);
  115.                error("Copyright 1988 S.E. Margison");
  116.             case '?':
  117.                usage();
  118.             default:
  119.                fputc(argv[argc][--i], stderr);
  120.                error(": invalid option");
  121.             }  /* end of switch */
  122.          } /* end of inner while */
  123.       }  /* end of outer while */
  124.    }
  125.  
  126. MKdate()
  127. {
  128.    int i, mpos, dpos;
  129.    char *mstr, dstr[6];
  130.    mpos = 0;
  131.    dpos = 3;
  132.    if(lf) {
  133.       dstr[0] = (ptm->tm_mday / 10) | '0';
  134.       if(dstr[0] == '0') dstr[0] = ' ';
  135.       dstr[1] = (ptm->tm_mday % 10) | '0';
  136.       dstr[2] = NULL;
  137.       if(nf) {
  138.          i = weekday(ptm->tm_mon+1, ptm->tm_mday, ptm->tm_year+1900);
  139.          mstr = wkdayname(i);
  140.          strcpy(datestr, mstr);
  141.          strcat(datestr, ", ");
  142.          }
  143.       mstr = monthis(ptm->tm_mon);
  144.       if(ef) {
  145.          strcat(datestr, dstr);
  146.          strcat(datestr, " ");
  147.          strcat(datestr, mstr);
  148.          }
  149.       else {
  150.          strcat(datestr, mstr);
  151.          strcat(datestr, " ");
  152.          strcat(datestr, dstr);
  153.          }
  154.       strcat(datestr, ", ");
  155.       i_dstr(dstr, ptm->tm_year+1900);
  156.       strcat(datestr, dstr);
  157.       return;
  158.       }
  159.  
  160.    if(ef) { mpos = 3; dpos = 0; }
  161.  
  162.    ptm->tm_mon++;    /* insure it is in range 1-12 */
  163.    datestr[mpos++] = (ptm->tm_mon / 10) | '0';
  164.    datestr[mpos] = (ptm->tm_mon % 10) | '0';
  165.    datestr[2] = datestr[5] = '/';
  166.    datestr[dpos++] = (ptm->tm_mday / 10) | '0';
  167.    datestr[dpos] = (ptm->tm_mday % 10) | '0';
  168.    i = ptm->tm_year;
  169.    if(i > 100) i -= 100;
  170.    datestr[6] = (i / 10) | '0';
  171.    datestr[7] = (i % 10) | '0';
  172.    datestr[8] = NULL;
  173.    }
  174.  
  175. MKtime()
  176. {
  177.    int hrs, eos;
  178.    timestr[2] = ':';
  179.    timestr[3] = (ptm->tm_min / 10) | '0';
  180.    timestr[4] = (ptm->tm_min % 10) | '0';
  181.    if(sf) {
  182.       timestr[5] = ':';
  183.       timestr[6] = (ptm->tm_sec / 10) | '0';
  184.       timestr[7] = (ptm->tm_sec % 10) | '0';
  185.       eos = 8;
  186.       }
  187.    else eos = 5;
  188.    if(mf) {
  189.       timestr[0] = (ptm->tm_hour / 10) | '0';
  190.       timestr[1] = (ptm->tm_hour % 10) | '0';
  191.       timestr[eos] = NULL;
  192.       }
  193.    else {
  194.       timestr[eos++] = ' ';
  195.       timestr[eos] = 'A';
  196.  
  197.       if(ptm->tm_hour >= 12) timestr[eos] = 'P';  /* set PM */
  198.  
  199.       if(ptm->tm_hour > 12) hrs = ptm->tm_hour - 12;
  200.       else if(ptm->tm_hour == 0) hrs = 12;   /* midnight = 12AM */
  201.       else hrs = ptm->tm_hour;
  202.  
  203.       timestr[++eos] = 'M';
  204.       timestr[++eos] = NULL;
  205.       timestr[0] = (hrs / 10) | '0';
  206.       timestr[1] = (hrs % 10) | '0';
  207.       if(timestr[0] == '0') timestr[0] = ' ';
  208.       }
  209.    }
  210.  
  211. usage() {
  212. puts("GTOD <options> or <-options> or </options>");
  213. puts("redirect stdout as desired");
  214. puts("d   report (D)ate only");
  215. puts("t   report (T)ime only");
  216. puts("s   report (S)econds (only if time is enabled)");
  217. puts("l   Report date in (L)ong format  (September 10, 1986)");
  218. puts("n   Include the (N)ame of the weekday, if -l format");
  219. puts("m   report time in (M)ilitary format (else, 12 hour format with AM/PM)");
  220. puts("e   report date in (E)uropean format  (DD/MM/YY)");
  221. puts("r   for time and date reporting, (R)everse order to time first");
  222. puts("    No options is the same as -dt:  MM/DD/YY hh:mm");
  223. puts("v   Version number and copyright message");
  224. puts("?   This help message");
  225. aabort(BEL);
  226.    }
  227.